import request from "./request";

// 用户操作
// 登录
export const loginApi = (params) => request.post("/users/login", params);
// 注册
export const regApi = (params) => request.post("/users/reg", params);
// 注销删除用户
export const deleteUserApi = (params) =>
  request.post("/users/deleteUser", params);
// 获取单个用户
export const getUserApi = (params) => request.post("/users/getUser", params);
// 修改用户密码
export const changeUserinfoApi = (params) =>
  request.post("/users/updateUser", params);

// 文件操作
// 文件列表
export const fileListApi = (params) =>
  request.post("/network/fileList", params);
// 搜索
export const searchApi = (params) => request.post("/network/search", params);
// 文件上传
export const uploadApi = (username, formData) =>
  request.post(`/fileUploadDownload/upload/${username}`, formData);

// 获取上传配置
export const getUploadConfigApi = () =>
  request.get("/fileUploadDownload/upload-config");

// 文件下载
export const downloadApi = (username, params) =>
  request.post(`/fileUploadDownload/download/${username}`, params, {
    responseType: 'blob'
  });



// 移动文件
export const moveApi = (params) => request.post("/network/move", params);

// 恢复
export const recoverApi = (params) => request.post("/network/recover", params);
// 删除
export const deleteApi = (params) => request.post("/network/delete", params);
// 永久删除
export const deleteForeverApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post(`/network/deleteForever/${username}`, params);
};

// 文件夹操作
// 创建文件夹
export const addFolderApi = (params) => request.post("/folder", params);
// 获取文件夹列表
export const getFolderApi = (params) =>
  request.post("/folder/getFolder", params);
// 删除文件夹
export const deleteFolderApi = (params) =>
  request.post("/folder/deleteFolder", params);
// 重命名文件夹
export const renameFolderApi = (params) =>
  request.post("/folder/renameFolder", params);
// 获取单个文件夹下的所有文件列表
export const getFilesApi = (params) => request.post("/folder/getFiles", params);

// 个人中心
// 预览 - 严格按照其他接口的模式，但支持文本预览
export const previewApi = (params) => request.post("/preview/preview", params, {
  responseType: 'blob'
});
// 内存容量
export const memoryApi = (params) => request.post("/network/memory", params);
// 回收站
export const recycleApi = (params) => request.post("/network/recycle", params);
// 创建分享
export const createShareApi = (params) =>
  request.post("/network/share", params);

// 获取我的分享列表
export const getMySharesApi = () =>
  request.post("/network/myShares");

// 取消分享
export const cancelShareApi = (params) =>
  request.post("/network/cancelShare", params);

// 通过分享码访问分享（无需认证）
export const accessShareApi = (shareCode, password = null) => {
  const url = `/network/share/${shareCode}${password ? `?password=${password}` : ''}`;
  return request.get(url);
};

// 兼容旧的分享API
export const shareApi = (params) => {
  return createShareApi({ fileId: params.id });
};
// 分享的作者
export const shareAuthorApi = (params) =>
  request.post("/network/shareAuthor", params);

// 文件类型
// 获取视频文件
export const getVideoApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post("/myfile/video", { ...params, username });
};

// 获取图片文件
export const getImgApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post("/myfile/image", { ...params, username });
};

// 获取文档文件
export const getDocApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post("/myfile/doc", { ...params, username });
};

// 获取音频文件
export const getAudioApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post("/myfile/audio", { ...params, username });
};

// 获取其他文件
export const getOtherApi = (params) => {
  const username = localStorage.getItem("username");
  return request.post("/myfile/other", { ...params, username });
};
